/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package classcreator.classfillers;

import classcreator.abstractclasses.AbstractClass;
import java.util.ArrayList;

/**
 *
 * @author Samus
 */
public class ABMMeth {

    private static ABMMeth instance = new ABMMeth();

    private ABMMeth() {
    }

    public static ABMMeth getInstance() {
        return instance;
    }

    public void addMethods(AbstractClass clase) {
        for (String s : addInserter(clase)) {
            clase.getAssembledMethods().add(s);
        }
        for (String s : addRemover(clase)) {
            clase.getAssembledMethods().add(s);
        }
        for (String s : addUpdater(clase)) {
            clase.getAssembledMethods().add(s);
        }
    }

    public void addSingleton(AbstractClass c) {
        String res = "private static " + c.getNombreclase() + " instance = new " + c.getNombreclase() + "();";
        c.getAssembledClass().add(res);
        res = "private " + c.getNombreclase() + "(){";
        c.getAssembledClass().add(res);
        res = "}";
        c.getAssembledClass().add(res);
        c.getAssembledClass().add("");
        res = "public static " + c.getNombreclase() + " getInstance() {";
        c.getAssembledClass().add(res);
        res = " return instance;";
        c.getAssembledClass().add(res);
        res = "}";
        c.getAssembledClass().add(res);
        c.getAssembledClass().add("");
    }

    public void addMainMeth(AbstractClass c) {
        String res = "public static void main(String[] args) {";
        c.getAssembledClass().add(res);
        res = "}";
        c.getAssembledClass().add(res);
    }

    public ArrayList<String> addInserter(AbstractClass clase) {
        ArrayList<String> res = new ArrayList<String>();
        String arres = "public void insertNew" + clase.getNombreclase() + " (";
        int i = 0;
        while (i < clase.getProperties().size()) {
            arres += Sorters.getInstance().propSort(clase.getProperties().get(i).getType()) + " new" + clase.getProperties().get(i).getName();
            if (clase.getProperties().size() >= 2) {
                if (i != clase.getProperties().size() - 1) {
                    arres += ", ";
                }
            }
            i++;
        }
        arres += "){";
        res.add(arres);
        String queryReal = "String query = \"" + clase.getSQLSentenceAdd() + "\";"; //SENTENCIA CON COMILLAS ESCAPADAS!!!!
        res.add(queryReal);
        for (String s : addDBStates()) {
            res.add(s);
        }
        res.add("}");
        return res;
    }

    public ArrayList<String> addRemover(AbstractClass clase) {
        ArrayList<String> res = new ArrayList<String>();
        String arres = "public void deleteNew" + clase.getNombreclase() + " (";
        int i = 0;
        while (i < clase.readPKs().size()) {
            arres += Sorters.getInstance().propSort(clase.readPKs().get(i).getType()) + " new" + clase.readPKs().get(i).getName();
            if (i != clase.readPKs().size() - 1) {
                arres += ", ";
            }
            i++;
        }
        arres += "){";
        res.add(arres);
        String queryReal = "String query = \"" + clase.getSQLSentenceDelete() + "\";";
        res.add(queryReal);
        for (String s : addDBStates()) {
            res.add(s);
        }
        res.add("}");
        return res;
    }

    public ArrayList<String> addUpdater(AbstractClass clase) {
        ArrayList<String> res = new ArrayList<String>();
        String arres = "public void updateNew" + clase.getNombreclase() + " (";
        int i = 0;
        while (i < clase.getProperties().size()) {
            arres += Sorters.getInstance().propSort(clase.getProperties().get(i).getType()) + " new" + clase.getProperties().get(i).getName();
            if (i != clase.getProperties().size() - 1) {
                arres += ", ";
            }
            i++;
        }
        arres += "){";
        res.add(arres);
        String queryReal = "String query = \"" + clase.getSQLSentenceModify() + "\";";
        res.add(queryReal);
        for (String s : addDBStates()) {
            res.add(s);
        }
        res.add("}");
        return res;
    }

    public ArrayList<String> addDBStates() {
        ArrayList<String> res = new ArrayList<String>();
        String conn = "Connection conn = DataBaseConnFactory.getInstance().obtenerNewConexion();";
        res.add(conn);
        String statement = "PreparedStatement ps = conn.prepareStatement(query);";
        res.add(statement);
        String exe = "ps.execute();";
        res.add(exe);
        return res;
    }
}
